/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logiclander.ricemine.services.identity;

import com.logiclander.ricemine.utils.LdapUtilities;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.kuali.rice.kim.bo.entity.dto.KimEntityPhoneInfo;
import org.kuali.rice.kim.util.KimConstants;
import org.springframework.ldap.core.ContextMapper;
import org.springframework.ldap.core.DirContextAdapter;

/**
 *
 * @author tcarroll
 */
public class LdapKimEntityPhoneInfoMapper implements ContextMapper {

  private final Log log = LogFactory.getLog(getClass());
  private String phoneNumberAttributeName = null;
  private String countryCodeAttributeName = null;
  private String extensionNumberAttributeName = null;
  private String formattedPhoneNumberAttributeName = null;
  private String suppressionAttributeName = null;

//beg: methods required by ContextMapper interface
  public Object mapFromContext(Object ctx) {
    validateConfiguration();
    KimEntityPhoneInfo kepi = new KimEntityPhoneInfo();
    try {
      DirContextAdapter context = (DirContextAdapter) ctx;
      // currently, this assumes that an entity only has one phone (the one aligned
      // with this ldap record.  this is a decent assumption for now, but a configurable
      // option for either doing it this way or mapping multiple phones would be better
      // perhaps some organizations have multi-valued attributes that list phones

      // the entityPhoneId is a synthetic identifier for the instance of an entity phone.
      // this should be unique in the system, but there is no graceful way to do this
      // from an ldap mapping
      kepi.setEntityPhoneId("1");//is there an ldap attribute for this?
      kepi.setEntityTypeCode("PERSON");//is there an ldap attribute for this?
      kepi.setPhoneTypeCode("WRK");//is there an ldap attribute for this?
      kepi.setPhoneNumberUnmasked(LdapUtilities.materialize(context.getStringAttribute(phoneNumberAttributeName)));
      kepi.setCountryCodeUnmasked(LdapUtilities.materialize(context.getStringAttribute(countryCodeAttributeName)));
      kepi.setFormattedPhoneNumber(LdapUtilities.materialize(context.getStringAttribute(formattedPhoneNumberAttributeName)));
      kepi.setExtensionNumberUnmasked(LdapUtilities.materialize(context.getStringAttribute(extensionNumberAttributeName)));

      String suppressionAttributeValue = context.getStringAttribute(suppressionAttributeName);
      if (suppressionAttributeValue != null && suppressionAttributeValue.equalsIgnoreCase("TRUE")) {
        kepi.setSuppressPhone(true);
        kepi.setPhoneNumber("000-000-0000");
        kepi.setCountryCode("XX");
        kepi.setFormattedPhoneNumber(KimConstants.RESTRICTED_DATA_MASK);
        kepi.setExtensionNumber(KimConstants.RESTRICTED_DATA_MASK);

      } else {
        kepi.setSuppressPhone(false);
        kepi.setPhoneNumber(LdapUtilities.materialize(context.getStringAttribute(phoneNumberAttributeName)));
        kepi.setCountryCode(LdapUtilities.materialize(context.getStringAttribute(countryCodeAttributeName)));
        kepi.setFormattedPhoneNumber(LdapUtilities.materialize(context.getStringAttribute(formattedPhoneNumberAttributeName)));
        kepi.setExtensionNumber(LdapUtilities.materialize(context.getStringAttribute(extensionNumberAttributeName)));
      }
      kepi.setActive(true);//is there an ldap attribute for this?
      kepi.setDefault(true);//is there an ldap attribute for this?
    } catch (Throwable t) {
      log.error("Error in ContextMapper", t);
      String msg = "ContextMapper failed to create a LdapRecord from the specified Context: " + ctx;
      throw new RuntimeException(msg, t);
    }
    if (log.isDebugEnabled()) {
      log.debug("mapped LdapKimEntityPhone:"
              + "(entityPhoneId=" + kepi.getEntityPhoneId() + ")"
              + "(phoneTypeCode=" + kepi.getPhoneTypeCode() + ")"
              + "(phoneNumber=" + kepi.getPhoneNumber() + ")"
              + "(isActive=" + kepi.isActive() + ")"
              + "(isDefault=" + kepi.isDefault() + ")"
              + "(isSuppressPhone=" + kepi.isSuppressPhone() + ")");
    }
    return kepi;
  }

//end: methods required by ContextMapper interface
//beg: helper methods
  public String getPhoneNumberAttributeName() {
    return this.phoneNumberAttributeName;
  }

  public void setPhoneNumberAttributeName(String attrName) {
    this.phoneNumberAttributeName = attrName;
  }

  public String getCountryCodeAttributeName() {
    return this.countryCodeAttributeName;
  }

  public void setCountryCodeAttributeName(String attrName) {
    this.countryCodeAttributeName = attrName;
  }

  public String getExtensionNumberAttributeName() {
    return this.extensionNumberAttributeName;
  }

  public void setExtensionNumberAttributeName(String attrName) {
    this.extensionNumberAttributeName = attrName;
  }

  public String getFormattedPhoneNumberAttributeName() {
    return this.formattedPhoneNumberAttributeName;
  }

  public void setformattedPhoneNumberAttributeName(String attrName) {
    this.formattedPhoneNumberAttributeName = attrName;
  }

  public String getSuppressionAttributeName() {
    return this.suppressionAttributeName;
  }

  public void setSuppressionAttributeName(String attrName) {
    this.suppressionAttributeName = attrName;
  }

  private void validateConfiguration() {
    if (phoneNumberAttributeName == null) {
      String msg = "The property 'phoneNumberAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (countryCodeAttributeName == null) {
      String msg = "The property 'countryCodeAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (extensionNumberAttributeName == null) {
      String msg = "The property 'extensionNumberAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (formattedPhoneNumberAttributeName == null) {
      String msg = "The property 'formattedPhoneNumberAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (suppressionAttributeName == null) {
      String msg = "The property 'suppressionAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
  }
//end: helper methods
}
